All right, before we continue setting up the storyline for our game, I want to add some features to
our players characters.
First, one of these features we can do to improve the ambiance in our game is to add footsteps to the
player, and this is relatively easy to do.
All we need to do is fill out our local script that we have inside of starters character scripts that
will be responsible for handling all of the character's footsteps.
Now, if you remember inside of our player, join service.
When a player's character gets added, we add a new instance or sound instance into that player's root
part, and it's called footsteps.
And this is the sound instance that we're going to be using to replace the default footstep sound that
exists within a player's character.
There's another sound instance which is called a running underneath a player's humanoid root part.
And we want to disable that sound and instead add functionality for our custom footstep sound so we
can go inside of our footsteps handler.
And in here we're only going to need two services.
One is going to be replicated storage.
And then the other service we'll need is the player service.
Now, the first variable we'll need is a reference to a module script we have inside of replicated storage.
So inside of here we have a footstep module.
And this was created by another developer.
And what it does is that it stores a bunch of different sound IDs depending on the surface a player
is walking on.
So we have stuff for like sand, snow, tile, wood and things of that.
And there's a material map in here.
So what we do is we pass a particular enum material that a player is walking on.
And from that we get a table of sounds that would exist for that particular material.
And then we get those sounds and we play a random one of those sound IDs for this particular floor material.
So we can go ahead and require this module script.
We'll call it footstep module require from replicated storage dot modules.
Dot footstep module.
And then we want to make a reference to the local player.
So players dot local player.
And then we want to get a reference to the player's character.
So we could do player dot character or we can do player dot character added weight in case the player's
character hasn't been replicated yet.
And then we want to get a reference to the humanoid in our character.
So character weight for child humanoid.
And then we want to get a reference to the root part of the player.
So character wait for child.
Humanoid root part.
And then I'm going to create a variable to keep track of the last material that our player was standing
on, so we can call it last material.
And for now we're going to set it to nil.
Now we're going to make some references to the sounds in the player.
So one is going to be the default running sound, which is on our root part.
And we're going to wait for the running sound.
And then we also want to make a reference to our custom footstep sound.
Which is also in our root part, and its name is footsteps.
Now, some other things we're going to want to look at is we want to get the walk speed of our character.
So walk speed is equal to our humanoid.
And we'll get the walk speed property.
And then we'll have a variable to keep track of our current walk speed.
And we'll set that to zero for now.
We're going to have a variable that will act as a cooldown, because we're going to have a while loop
that will constantly check if we are walking.
And if we're walking it'll play a footstep sound, but we don't want to constantly play a footstep sound.
We want to play a footstep sound.
Depending on how fast our player is walking.
And by default, we're going to have a cooldown of 0.35 seconds between each footstep sound.
And then last but not least, we're going to have a volume for our footstep sounds as well, and we'll
just set that to something like 0.3.
And the last but not least, what we want to do is we want to set the running sound dot volume equal
to zero, because we don't want to worry about Roblox's default implementation of the running sound.
We want to go ahead and make sure it can't be heard at all.
Now, some private functions we're going to need in here.
Uh, one we need is to check the movement of our player.
And then another function we're going to need is to get the material that the player is standing on.
And from that, we can return a table containing all of the different sound IDs based on that material.
So we can call this get material, and we'll go ahead and do that stuff in there.
Now another thing we want to listen in the handler section is when the walk speed of our player changes.
So we can do humanoid, get property, change signal walk speed.
And when this walks be changes, we want to update our cool down to a different number.
So for example, if the humanoids walk speed.
Is greater than the original walk speed we stored in that variable.
Then we want to set our cooldown to be something shorter, so that way we can play footsteps faster
so we can set it to something like 0.25.
Otherwise, if our walk speed is less than our original walk speed, then we want to reduce the cooldown
so we could set it to something like 0.45.
Otherwise, if our walk speed stayed the same, then we can just keep our cooldown at 0.35, which is
the default we set up here.
Another event we want to listen to is when our humanoid actually starts to move, and there's an event
in our humanoid called running, and this gets fired when the player starts to move.
So we're going to connect a function to this, and this function gets passed an integer that represents
the speed of the player.
And all we're going to do in here is we're going to set the current speed variable equal to this speed.
All right.
So now in our main section here we're going to create a while loop.
So while true what we're going to do is we're going to wait for our cooldown.
And then after that's up we're going to check if our player is moving using our check movement private
function.
So we can have this function return a boolean that will tell us whether or not the player is moving.
So if check movement is true then we can play a footstep sound.
So let's go ahead and fill out this function here.
So what we're going to do is we're going to set the last material variable equal to the humanoid's current
floor material.
So there's a property called floor material.
And actually if I go up here where I get the human I can denote this as a humanoid.
And that way we can actually see all of the different properties and stuff for the humanoid.
So this will tell us the current floor material that the humanoid is standing on.
And then after that, what we're going to do is we're going to check if the humanoid dot walk speed
is greater than zero.
And we also want to make sure that the humanoid dot move direction describes the direction that the
humanoid is walking in.
We want to make sure that the magnitude of this move direction is greater than zero, because if it
isn't greater than zero, then the player isn't moving.
And then we also want to check whether or not the current speed of the player is also greater than zero.
Another condition we'll want to check is if the last material we are standing on is not equal to nil.
So we want to make sure that we're actually standing on something.
And we also want to make sure that the last material is not equal to the enum dot material dot air.
If all of that checks out, then what we could do here is return true.
Otherwise we'll just return false.
So this function is going to verify that a our humanoids walk speed is greater than zero.
The humanoid is actually moving.
The humanoid actually has a speed that they are moving with, and that they're actually standing on
a particular surface.
And if that's true, then what we could do is play a sound.
So what we're going to do is we're going to get our footstep sound, and we're going to set the sound
ID equal to whatever gets returned from our get material function.
So we can call our get material function to get a particular sound ID.
So that means what we need to do in this function is that we need to get a table from our footstep module,
which is the sound table.
So we can do footstep module.
And there's a function in there called get table from material.
And the material we want to pass is the last material we were standing on.
And then after that we can get a random sound out of this table.
So we could do random sound is equal to footstep module.
And there's a function in there to allow us to get a random sound.
And all we need to do is pass this sound table.
And then after that we can return this sound ID.
Now that we have returned the sound ID and we set it on our footstep sound instance, then what we could
do is we could set footstep sound dot volume, make sure it is equal to the volume variable, and then
we can just go ahead and play the footstep sound.
So now it means we should be able to test whether or not our player has footsteps.
So if we go into playtesting our game.
And we spawn in here if I walk on this surface.
As you can see, we here sand.
If I walk on to the spawn plates, we should hear a different sound.
Perfect.
Now, if I go ahead and increase the speed of my player's character.
So if I go to my humanoid and actually let me do this on the server so it replicates, if I go to my
character, go to my humanoid, and I increase my walk speed to something, let's say like, uh, 24,
then if I go back to my client, the footstep, uh, sound rate should be increased.
So now if I walk around.
As you can see, the footsteps are playing much more frequently than previously.
All right.
And that's all we needed to do in this lecture.
In the next lecture, we're going to continue adding some neat functionality to our players character.
So I'll go ahead and see you there.